﻿using App.Desk.Extend;
using App.Desk.UserControls;
using App.Untiys;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App.Desk.Pro.Monitors
{
    public partial class FrmRunData : UIPage
    {
        public List<PLCParaModel> plcParas = new List<PLCParaModel>();
        public FrmRunData()
        {
            InitializeComponent();
            SetInfo();
            SamplingPLCHelper.Instance.PlcDataReadOut += OnPlcDataReadOut;
        }

        public void SetInfo()
        {
            List<string> paraList = new List<string>();
            paraList.Add("当前采样点|VW|14");
            if (Config.CurrentUser.IsAdmin == 1)
                paraList.Add("当前采样桶号|VW|10");
            paraList.Add("大车当前坐标|VW|0");
            paraList.Add("小车当前坐标|VW|2");
            paraList.Add("升降当前坐标|VW|6");
            paraList.Add("大车目标坐标|VW|66");
            paraList.Add("小车目标坐标|VW|68");
            paraList.Add("升降目标坐标|VW|70");
            paraList.Add("目标采样点数|VW|72");
            if (Config.CurrentUser.IsAdmin == 1)
                paraList.Add("目标采样桶号|VW|74");
            paraList.Add("缩分时间反馈|VW|16");
            paraList.Add("制样时间反馈|VW|76");
            paraList.Add("放样点数反馈|VW|80");
            paraList.Add("缩分时间间隔|VW|276");

            paraList.ForEach(res =>
            {
                var array = res.ToSplitArrayEx('|');
                plcParas.Add(new PLCParaModel
                {
                    Name = array[0],
                    Region = array[1],
                    Addr = array[2],
                });
            });
        }

        private void FrmRunData_Load(object sender, EventArgs e)
        {
            UcInfoButton ucInfoButton;
            foreach (var item in plcParas)
            {
                ucInfoButton = new UcInfoButton();
                ucInfoButton.SetText(item.Name + ":");
                ucInfoButton.SetValue("");
                ucInfoButton.Width = 180;
                ucInfoButton.Parmeter = item;
                flowBox.Add(ucInfoButton);
            }
        }

        private void OnPlcDataReadOut(object send, ActiveReadEventArgs e)
        {
            short[] _arrReads = (short[])e.E_ReadValue;
            foreach (var control in flowBox.GetAllControls())
            {
                if (control is UcInfoButton)
                {
                    try
                    {
                        string result = "";
                        var item = ((UcInfoButton)control);
                        if (item.Parmeter.Region == "V")
                        {
                            int[] _aryBits;
                            var ints = item.Parmeter.Addr.To_PLC_V_SPLIT();
                            if (ints.IsNotNull())
                            {
                                _aryBits = DataConvert.Ushort2Binary((int)(_arrReads[ints[0] / 2]));
                                result = _aryBits[ints[1]].ToStringEx();
                            }
                        }
                        if (item.Parmeter.Region == "VW")
                        {
                            if (item.Parmeter.Addr.ToPLCIntEx() == 276)
                                result = SamplingPLCHelper.Instance.DoRead(276).ToStringEx();
                            else
                                result = _arrReads[(item.Parmeter.Addr.ToPLCIntEx()) / 2].ToStringEx();
                        }
                        UIHelper.SetInfoControl((UcInfoButton)control, result);
                    }
                    catch
                    {

                    }
                }
            }
        }
    }
}
